'use strict';

const modelCheckQuery = {
  name: {
    type: 'string',
    min: 1,
    max: 100,
  },
  url: {
    type: 'string',
    max: 1000,
    required: false,
  },
  icon: {
    type: 'string',
    max: 1000,
    required: false,
  },
  detail: {
    type: 'string',
    required: false,
    max: 1000,
  },
  obligation: {
    type: 'string',
    max: 1000,
    required: false
  },
  startTime: {
    type: 'w-date',
    required: false
  },
  endTime: {
    type: 'w-date',
    required: false
  }
}

exports.index = async ctx => {
  const data = await ctx.service.work.list();
  ctx.sendSuccess(data);
};

exports.create = async ctx => {
  ctx.validate(modelCheckQuery);
  await ctx.service.work.create(ctx.w_body('name url icon detail obligation startTime endTime'))
  ctx.sendSuccess();
};

exports.update = async ctx => {
  ctx.validate(modelCheckQuery);
  ctx.validate({
    id: {
      type: 'mongo-id',
    },
  }, ctx.params);
  ctx.sendSuccess(await ctx.service.work.update(ctx.params.id, ctx.w_body('name url icon detail obligation startTime endTime')));
};

exports.destroy = async ctx => {
  ctx.validate({
    id: {
      type: 'mongo-id',
    },
  }, ctx.params);
  await ctx.service.work.destroy(ctx.params.id)
  ctx.sendSuccess();
};

